home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / make_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-28  |  12.8 KB  |  527 lines

  1. /* make_cmd.c --
  2.    Functions for making instances of the various parser constructs. */
  3.  
  4. /* Copyright (C) 1989 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/file.h>
  25. #include "config.h"
  26. #include "general.h"
  27. #include "error.h"
  28. #include "command.h"
  29. #include "flags.h"
  30. #include "filecntl.h"
  31.  
  32. #if defined (JOB_CONTROL)
  33. #include "jobs.h"
  34. #endif
  35.  
  36. WORD_DESC *
  37. make_word (string)
  38.      char *string;
  39. {
  40.   WORD_DESC *temp;
  41.  
  42.   temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  43.   temp->word = savestring (string);
  44.   temp->quoted = temp->dollar_present = temp->assignment = 0;
  45.  
  46.   while (*string)
  47.     {
  48.       if (*string == '$') temp->dollar_present = 1;
  49.  
  50.       if (member (*string, "'`\\\""))
  51.     {
  52.       temp->quoted = 1;
  53.       if (*string == '\\')
  54.         string++;
  55.     }
  56.  
  57.       if (*string)
  58.     (string++);
  59.     }
  60.   return (temp);
  61. }
  62.  
  63. WORD_DESC *
  64. make_word_from_token (token)
  65.      int token;
  66. {
  67.   char tokenizer[2];
  68.  
  69.   tokenizer[0] = token;
  70.   tokenizer[1] = '\0';
  71.  
  72.   return (make_word (tokenizer));
  73. }
  74.  
  75. WORD_LIST *
  76. make_word_list (word, link)
  77.      WORD_DESC *word;
  78.      WORD_LIST *link;
  79. {
  80.   WORD_LIST *temp;
  81.  
  82.   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  83.   temp->word = word;
  84.   temp->next = link;
  85.   return (temp);
  86. }
  87.  
  88. WORD_LIST *
  89. add_string_to_list (string, list)
  90.      char *string;
  91.      WORD_LIST *list;
  92. {
  93.   WORD_LIST *temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  94.   temp->word = make_word (string);
  95.   temp->next = list;
  96.   return (temp);
  97. }
  98.  
  99. WORD_DESC *
  100. coerce_to_word (number)
  101.      int number;
  102. {
  103.   char *string;
  104.  
  105.   string = (char *)alloca (24);
  106.   sprintf (string, "%d", number);
  107.   return (make_word (string));
  108. }
  109.  
  110. COMMAND *
  111. make_command (type, pointer)
  112.      enum command_type type;
  113.      SIMPLE_COM *pointer;
  114. {
  115.   COMMAND *temp;
  116.  
  117.   temp = (COMMAND *)xmalloc (sizeof (COMMAND));
  118.   temp->type = type;
  119.   temp->value.Simple = pointer;
  120.   temp->value.Simple->flags = 0;
  121.   temp->flags = 0;
  122.   temp->redirects = (REDIRECT *)NULL;
  123.   return (temp);
  124. }
  125.  
  126. COMMAND *
  127. command_connect (com1, com2, connector)
  128.      COMMAND *com1, *com2;
  129.      int connector;
  130. {
  131.   CONNECTION *temp;
  132.  
  133.   temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
  134.   temp->connector = connector;
  135.   temp->first = com1;
  136.   temp->second = com2;
  137.   return (make_command (cm_connection, (SIMPLE_COM *)temp));
  138. }
  139.  
  140. COMMAND *
  141. make_for_command (name, map_list, action)
  142.      WORD_DESC *name;
  143.      WORD_LIST *map_list;
  144.      COMMAND *action;
  145. {
  146.   FOR_COM *temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
  147.  
  148.   temp->flags = 0;
  149.   temp->name = name;
  150.   temp->map_list = map_list;
  151.   temp->action = action;
  152.   return (make_command (cm_for, (SIMPLE_COM *)temp));
  153. }
  154.  
  155. COMMAND *
  156. make_group_command (command)
  157.      COMMAND *command;
  158. {
  159.   GROUP_COM *temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
  160.  
  161.   temp->command = command;
  162.   return (make_command (cm_group, (SIMPLE_COM *)temp));
  163. }
  164.  
  165. COMMAND *
  166. make_case_command (word, clauses)
  167.      WORD_DESC *word;
  168.      PATTERN_LIST *clauses;
  169. {
  170.   extern GENERIC_LIST *reverse_list ();
  171.   CASE_COM *temp;
  172.  
  173.   temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
  174.   temp->flags = 0;
  175.   temp->word = word;
  176.   temp->clauses = (PATTERN_LIST *)reverse_list (clauses);
  177.   return (make_command (cm_case, (SIMPLE_COM *)temp));
  178. }
  179.  
  180. PATTERN_LIST *
  181. make_pattern_list (patterns, action)
  182.      WORD_LIST *patterns;
  183.      COMMAND *action;
  184. {
  185.   PATTERN_LIST *temp;
  186.  
  187.   temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
  188.   temp->patterns = patterns;
  189.   temp->action = action;
  190.   temp->next = NULL;
  191.   return (temp);
  192. }
  193.  
  194. COMMAND *
  195. make_if_command (test, true_case, false_case)
  196.      COMMAND *test, *true_case, *false_case;
  197. {
  198.   IF_COM *temp;
  199.  
  200.   temp = (IF_COM *)xmalloc (sizeof (IF_COM));
  201.   temp->flags = 0;
  202.   temp->test = test;
  203.   temp->true_case = true_case;
  204.   temp->false_case = false_case;
  205.   return (make_command (cm_if, (SIMPLE_COM *)temp));
  206. }
  207.  
  208. COMMAND *
  209. make_until_or_while (test, action, which)
  210.      COMMAND *test, *action;
  211.      enum command_type which;
  212. {
  213.   WHILE_COM *temp;
  214.  
  215.   temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
  216.   temp->flags = 0;
  217.   temp->test = test;
  218.   temp->action = action;
  219.   return (make_command (which, (SIMPLE_COM *)temp));
  220. }
  221.  
  222. COMMAND *
  223. make_while_command (test, action)
  224.      COMMAND *test, *action;
  225. {
  226.   return (make_until_or_while (test, action, cm_while));
  227. }
  228.  
  229. COMMAND *
  230. make_until_command (test, action)
  231.      COMMAND *test, *action;
  232. {
  233.   return (make_until_or_while (test, action, cm_until));
  234. }
  235.  
  236. COMMAND *
  237. make_bare_simple_command ()
  238. {
  239.   COMMAND *command;
  240.   SIMPLE_COM *temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
  241.  
  242.   temp->flags = 0;
  243.   temp->words = (WORD_LIST *)NULL;
  244.   temp->redirects = (REDIRECT *)NULL;
  245.   command = (COMMAND *)xmalloc (sizeof (COMMAND));
  246.   command->type = cm_simple;
  247.   command->redirects = (REDIRECT *)NULL;
  248.   command->flags = 0;
  249.   command->value.Simple = temp;
  250.   return (command);
  251. }
  252.  
  253. /* Return a command which is the connection of the word or redirection
  254.    in ELEMENT, and the command * or NULL in COMMAND. */
  255. COMMAND *
  256. make_simple_command (element, command)
  257.      ELEMENT element;
  258.      COMMAND *command;
  259. {
  260.   /* If we are starting from scratch, then make the initial command
  261.      structure.  Also note that we have to fill in all the slots, since
  262.      malloc doesn't return zeroed space. */
  263.   if (!command)
  264.     command = make_bare_simple_command ();
  265.  
  266.   if (element.word)
  267.     {
  268.       WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  269.       tw->word = element.word;
  270.       tw->next = command->value.Simple->words;
  271.       command->value.Simple->words = tw;
  272.     }
  273.   else
  274.     {
  275.       REDIRECT *r = element.redirect;
  276.       /* Due to the way <> is implemented, there may be more than a single
  277.      redirection in element.redirect.  We just follow the chain as far
  278.      as it goes, and hook onto the end. */
  279.       while (r->next)
  280.     r = r->next;
  281.       r->next = command->value.Simple->redirects;
  282.       command->value.Simple->redirects = element.redirect;
  283.     }
  284.   return (command);
  285. }
  286.  
  287. #define POSIX_HERE_DOCUMENTS
  288. make_here_document (temp)
  289.      REDIRECT *temp;
  290. {
  291.   int kill_leading = 0;
  292.  
  293.   switch (temp->instruction)
  294.     {
  295.       /* Because we are Bourne compatible, we read the input for this
  296.      << or <<- redirection now, from wherever input is coming from.
  297.      We store the input read into a WORD_DESC.  Replace the text of
  298.      the redirectee.word with the new input text.  If <<- is on,
  299.      then remove leading TABS from each line. */
  300.  
  301.       case r_deblank_reading_until:    /* <<-foo */
  302.     kill_leading++;
  303.     /* ... */
  304.       case r_reading_until:        /* <<foo */
  305.     {
  306.       extern char *redirection_expand ();
  307.       extern char *string_quote_removal ();
  308.       char *redirectee_word;
  309.       int len;
  310.  
  311.       char *document = (char *)NULL;
  312.       int document_index = 0, document_size = 0;
  313.  
  314. #if !defined (POSIX_HERE_DOCUMENTS)
  315.       /* Because of Bourne shell semantics, we turn off globbing, but
  316.          only for this style of redirection.  I feel a little ill.  */
  317.       {
  318.         extern int disallow_filename_globbing;
  319.         int old_value = disallow_filename_globbing;
  320.         disallow_filename_globbing = 1;
  321.  
  322.         redirectee_word = redirection_expand (temp->redirectee.filename);
  323.  
  324.         disallow_filename_globbing = old_value;
  325.       }
  326. #else /* POSIX_HERE_DOCUMENTS */
  327.       /* Quote removal is the only expansion performed on the delimiter
  328.          for here documents, making it an extremely special case.  I
  329.          still feel ill. */
  330.       redirectee_word =
  331.         string_quote_removal (temp->redirectee.filename->word, 0);
  332. #endif /* POSIX_HERE_DOCUMENTS */
  333.  
  334.       /* redirection_expand will return NULL if the expansion results in
  335.          multiple words or no words.  Check for that here, and just abort
  336.          this here document if it does. */
  337.       if (redirectee_word)
  338.         len = strlen (redirectee_word);
  339.       else
  340.         {
  341.           temp->here_doc_eof = savestring ("");
  342.           goto document_done;
  343.         }
  344.  
  345.       free (temp->redirectee.filename->word);
  346.       temp->here_doc_eof = redirectee_word;
  347.  
  348.       /* Read lines from wherever lines are coming from.
  349.          For each line read, if kill_leading, then kill the
  350.          leading tab characters.
  351.          If the line matches redirectee_word exactly, then we have
  352.          manufactured the document.  Otherwise, add the line to the
  353.          list of lines in the document. */
  354.       {
  355.         extern char *read_secondary_line ();
  356.         char *line;
  357.         int l;
  358.  
  359.         while (line = read_secondary_line ())
  360.           {
  361.         if (!line)
  362.           goto document_done;
  363.  
  364.         if (kill_leading)
  365.           {
  366.             register int i;
  367.  
  368.             /* Hack:  To be compatible with some Bourne shells, we 
  369.                check the word before stripping the whitespace.  This
  370.                is a hack, though. */
  371.             if ((strncmp (line, redirectee_word, len) == 0) &&
  372.             line[len] == '\n')
  373.               goto document_done;
  374.  
  375.             for (i = 0; line[i] == '\t'; i++)
  376.               ;
  377.  
  378.             if (i)
  379.               strcpy (&line[0], &line[i]);
  380.           }
  381.  
  382.         if ((strncmp (line, redirectee_word, len) == 0) &&
  383.             line[len] == '\n')
  384.           goto document_done;
  385.  
  386.         l = strlen (line);
  387.         if (l + document_index >= document_size)
  388.           {
  389.             document = (char *)
  390.               xrealloc (document, (document_size += (10 * l)));
  391.           }
  392.  
  393.         if (l != 0)
  394.           {
  395.             strcpy (&document[document_index], line);
  396.             free (line);
  397.             document_index += l;
  398.           }
  399.           }
  400.   document_done:
  401.         if (!document)
  402.           document = savestring ("");
  403.         temp->redirectee.filename->word = document;
  404.       }
  405.     }
  406.     }
  407. }
  408.    
  409. /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION. 
  410.    INSTRUCTION is the instruction type, SOURCE is an INT,
  411.    and DEST is an INT or a WORD_DESC *. */
  412. REDIRECT *
  413. make_redirection (source, instruction, dest)
  414.      enum r_instruction instruction;
  415. {
  416.   REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
  417.  
  418.   /* First do the common cases. */
  419.   temp->redirector = source;
  420.   temp->redirectee.dest = dest;
  421.   temp->instruction = instruction;
  422.   temp->next = (REDIRECT *)NULL;
  423.  
  424.   switch (instruction)
  425.     {
  426.  
  427.     case r_output_direction:    /* >foo */
  428.     case r_output_force:    /* >| foo */
  429.       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  430.       break;
  431.  
  432.     case r_input_direction:    /* <foo */
  433.     case r_inputa_direction:    /* foo & makes this. */
  434.       temp->flags = O_RDONLY;
  435.       break;
  436.  
  437.     case r_appending_to:    /* >>foo */
  438.       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
  439.       break;
  440.  
  441.     case r_deblank_reading_until: /* <<-foo */
  442.     case r_reading_until:    /* << foo */
  443.       break;
  444.  
  445.     case r_duplicating_input:        /* 1<&2 */
  446.     case r_duplicating_output:        /* 1>&2 */
  447.     case r_close_this:            /* <&- */
  448.     case r_duplicating_input_word:    /* 1<&$foo */
  449.     case r_duplicating_output_word:    /* 1>&$foo */
  450.       break;
  451.     
  452.     case r_err_and_out:        /* command &>filename */
  453.       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  454.       break;
  455.  
  456.     case r_input_output:
  457.       temp->flags = O_RDWR;
  458.       break;
  459.  
  460.     default:
  461.       programming_error ("Redirection instruction from yyparse () '%d' is\n\
  462. out of range in make_redirection ().", instruction);
  463.       abort ();
  464.       break;
  465.     }
  466.   return (temp);
  467. }
  468.  
  469. COMMAND *
  470. make_function_def (name, command)
  471.      WORD_DESC *name;
  472.      COMMAND *command;
  473. {
  474.   FUNCTION_DEF *temp;
  475.  
  476.   temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
  477.   temp->command = command;
  478.   temp->name = name;
  479.   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
  480. }
  481.  
  482. /* Reverse the word list and redirection list in the simple command
  483.    has just been parsed.  It seems simpler to do this here the one
  484.    time then by any other method that I can think of. */
  485. COMMAND *
  486. clean_simple_command (command)
  487.      COMMAND *command;
  488. {
  489.   extern GENERIC_LIST *reverse_list ();
  490.  
  491.   if (command->type != cm_simple)
  492.     {
  493.       programming_error
  494.     ("clean_simple_command () got a command with type %d.", command->type);
  495.     }
  496.   else
  497.     {
  498.       command->value.Simple->words =
  499.     (WORD_LIST *)reverse_list (command->value.Simple->words);
  500.       command->value.Simple->redirects = 
  501.     (REDIRECT *)reverse_list (command->value.Simple->redirects);
  502.     }
  503.  
  504.   return (command);
  505. }
  506.  
  507. /* Cons up a new array of words.  The words are taken from LIST,
  508.    which is a WORD_LIST *.  Absolutely everything is malloc'ed,
  509.    so you should free everything in this array when you are done.
  510.    The array is NULL terminated. */
  511. char **
  512. make_word_array (list)
  513.      WORD_LIST *list;
  514. {
  515.   int count = list_length (list);
  516.   char **array = (char **)xmalloc ((1 + count) * sizeof (char *));
  517.  
  518.   for (count = 0; list; count++)
  519.     {
  520.       array[count] = (char *)xmalloc (1 + strlen (list->word->word));
  521.       strcpy (array[count], list->word->word);
  522.       list = list->next;
  523.     }
  524.   array[count] = (char *)NULL;
  525.   return (array);
  526. }
  527.